<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class StaffSkill{
	
	static public $FLAG_INACTIVE = 0;
	
	static public $FLAG_UNACTIVE = 10;
	
	static public $TABLE_NAME="staff_skill";

	static public function Create($staffId, $serviceId, $resetTime = 5, $skillLevel = 1) {
		$staffSkill = array();
		$staffSkill['staff_id'] = $staffId;
		$staffSkill['service_id'] = $serviceId;
		$staffSkill['reset_device_time'] = $resetTime;
		$staffSkill['skill_level'] = $skillLevel;
		$staffSkill['create_time'] = time();
		$staffSkill['id'] = DB::Insert(self::$TABLE_NAME, $staffSkill);
		
		return $staffSkill['id'];
	}
	
	static public function Delete($staffId) {
		DB::Delete(self::$TABLE_NAME, " staff_id=$staffId ");
		
		return ;
	}
	
	static public function GetSkillsByStaffId($staffId){
		$oc = array( 
			'staff_id' => $staffId
		);
		$allShops = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc
		));
		return $allShops;
	}
	
	static public function GetStaffsBySkillAndLevle($serviceId, $skillLevle){
		$oc = array( 
			'service_id' => $serviceId,
			'skill_level' => $skillLevle
		);
		$allShops = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc
		));
		return $allShops;
	}
	
	static public function GetStaffsByNoSuchSkill($serviceId){
		$sql = "select distinct(u1.staff_id) from staff_skill u1 where u1.staff_id not in(select distinct(staff_id) from staff_skill  where service_id=$serviceId)";
		
		return DB::GetQueryResult($sql, false);
	}
	
	static public function GetServicesById($staffId) {
		$sql = " select services.id as id, services.service_name as service_name, staff_skill.skill_level as skill_level from staff_skill, services where "
		."  staff_skill.staff_id=$staffId and staff_skill.service_id=services.id ";
		//Logs::setLog(" staff skill s log is || $sql  ||  ");
		return DB::GetQueryResult($sql, false);
	}
	
	/*static public function GetServicesById($staffId) {
		$sql = " select count(1) from staff_skill where staff_id=$staffId";
		//Logs::setLog(" staff skill s log is || $sql  ||  ");
		return DB::GetQueryResult($sql, false);
	}*/
	
	static public function Update($staffSkill) {
		Table::UpdateCache(self::$TABLE_NAME, $staffSkill['id'], $staffSkill);
		
		return ;
	}
	
	static public function GetByStaffAndSkill($staffId, $skillId) {
		$oc = array("staff_id"=>$staffId, "service_id"=>$skillId);
		return DB::LimitQuery(self::$TABLE_NAME, array( "condition" => $oc ));
	}
	
	static public function DeleteBySS($staffId, $serviceId) {
		$sql = " delete from staff_skill where staff_id=$staffId and service_id=$serviceId ";
		return DB::Query($sql, false);
	}
	
	static public function GetStaffsBySkill($serviceId){
		$oc = array( 
			'service_id' => $serviceId
		);
		$allShops = DB::LimitQuery(self::$TABLE_NAME, array(
				'condition' => $oc
		));
		return $allShops;
	}
}
